package com.dimples.dd.system.controller;

import com.dimples.dd.common.constant.DDServerConstant;
import com.dimples.dd.common.pojo.PageResult;
import com.dimples.dd.common.result.CommonResult;
import com.dimples.dd.log.core.annotation.ApiLog;
import com.dimples.dd.log.core.enums.OperateTypeEnum;
import com.dimples.dd.system.model.form.GatewayRouteForm;
import com.dimples.dd.system.model.query.GatewayRoutePageQuery;
import com.dimples.dd.system.model.vo.GatewayRoutePageVO;
import com.dimples.dd.system.service.SystemGatewayRouteService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static com.dimples.dd.common.result.CommonResult.success;

@Slf4j
@Tag(name = "Gateway路由管理")
@RestController
@RequestMapping("gateway-route")
@Validated
@RequiredArgsConstructor
public class SysGatewayRouteController {

    private final SystemGatewayRouteService gatewayRouteService;

    @PostMapping
    @Operation(summary = "创建Gateway路由")
    @PreAuthorize("@ss.hasPerm('system:gateway-route:create')")
    @ApiLog(operateType = OperateTypeEnum.CREATE, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public CommonResult<Long> createGatewayRoute(@Valid @RequestBody GatewayRouteForm gatewayRouteForm) {
        return success(this.gatewayRouteService.createGatewayRoute(gatewayRouteForm));
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新Gateway路由")
    @PreAuthorize("@ss.hasPerm('system:gateway-route:update')")
    @ApiLog(operateType = OperateTypeEnum.UPDATE, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public CommonResult<Boolean> updateGatewayRoute(
            @PathVariable String id,
            @Valid @RequestBody GatewayRouteForm gatewayRouteForm) {
        this.gatewayRouteService.updateGatewayRoute(gatewayRouteForm);
        return success(true);
    }

    @DeleteMapping
    @Operation(summary = "删除Gateway路由")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPerm('system:gateway-route:delete')")
    @ApiLog(operateType = OperateTypeEnum.DELETE, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public CommonResult<Boolean> deleteGatewayRoute(@RequestParam("id") Long id) {
        this.gatewayRouteService.deleteGatewayRoute(id);
        return success(true);
    }

    @GetMapping("/{id}/form")
    @Operation(summary = "获得文件配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPerm('system:gateway-route:query')")
    @ApiLog(operateType = OperateTypeEnum.SELECT, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public CommonResult<GatewayRouteForm> selectGatewayRoute(@PathVariable("id") Long id) {
        GatewayRouteForm config = this.gatewayRouteService.selectGatewayRoute(id);
        return success(config);
    }

    @GetMapping("/page")
    @Operation(summary = "获得Gateway路由分页")
    @PreAuthorize("@ss.hasPerm('system:gateway-route:query')")
    @ApiLog(operateType = OperateTypeEnum.SELECT, applicationName = DDServerConstant.DD_SERVER_SYSTEM)
    public PageResult<GatewayRoutePageVO> selectGatewayRoutePage(@Valid GatewayRoutePageQuery pageQuery) {
        return this.gatewayRouteService.selectGatewayRoutePage(pageQuery);
    }
}
